﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace shop.Areas.admin.Controllers
{
    [Authorize]
    public class slideController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /admin/slide/

        public ActionResult Index()
        {
            return View(db.Slides.ToList());
        }

        //
        // GET: /admin/slide/Details/5

        public ActionResult Details(int id = 0)
        {
            Slide slide = db.Slides.Find(id);
            if (slide == null)
            {
                return HttpNotFound();
            }
            return View(slide);
        }

        //
        // GET: /admin/slide/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /admin/slide/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Slide slide,HttpPostedFileBase AnhSlide)
        {
            var path = Server.MapPath("/images/slide/");
            var da = DateTime.Now.ToString("yyyyddmmhhMMss");
            if(slide.link == null )
            {
                slide.link = "#";
            }
            if(AnhSlide != null )
            {
                slide.HinhAnh =da+ Path.GetFileName(AnhSlide.FileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                AnhSlide.SaveAs(path + slide.HinhAnh);
            }
            else
            {
                slide.HinhAnh = "noimages.jpg";
            }
            if (ModelState.IsValid)
            {
                db.Slides.Add(slide);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(slide);
        }

        //
        // GET: /admin/slide/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Slide slide = db.Slides.Find(id);
            if (slide == null)
            {
                return HttpNotFound();
            }
            return View(slide);
        }

        //
        // POST: /admin/slide/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Slide slide,string anhcu, HttpPostedFileBase AnhSlideMoi)

        {
            var path = Server.MapPath("/images/slide/");
            var da = DateTime.Now.ToString("yyyymmddhhMMss");

            if(AnhSlideMoi !=null )
            {
                slide.HinhAnh = da + Path.GetFileName(AnhSlideMoi.FileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                AnhSlideMoi.SaveAs(path  + slide.HinhAnh);
            }
            else
            {
                slide.HinhAnh = anhcu;
            }
            if (ModelState.IsValid)
            {
                db.Entry(slide).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(slide);
        }
        [HttpPost, ActionName("Delete")]        
        public ActionResult Delete(int id)
        {
            Slide slide = db.Slides.Find(id);
            try
            {
                db.Slides.Remove(slide);
                db.SaveChanges();
                return Content(Boolean.TrueString);
            }
             catch
            {
                return Content(Boolean.FalseString);
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}